#include "tools_curl.h"
#include <logger.h>

static void url_response( CUrlClient * client, void * args )
{
	fprintf( stderr, "******client id(%d) response*********\n", client->get_curlid() );
    fprintf( stderr, "get url(%s) response\n", client->get_cgi_fullpath().c_str() );
    fprintf( stderr, "result=(%d) reason=(%s)\n", client->get_result(), curl_easy_strerror( (CURLcode)client->get_result() ) );
    fprintf( stderr, "response content(%s)\n\n", client->get_content().c_str() );
    return;
}

#define T 5

int32_t main( int32_t argc, char * argv[] )
{
	init_dailylog( "curl_test", MsgLogger::ELOGLEVEL_DEBUG );
    int32_t t = T;
    CUrlMgr * curmgr_instance = new CUrlMgr();
    curmgr_instance->init( 3 );
	
	char * encode_data = curmgr_instance->urlencode( "你好！", strlen("你好！") );
	LOG_STREAM_DEBUG << "你好！ encode[" << encode_data << "]" << LOG_EOM;
	curl_free(  encode_data );
	encode_data = curmgr_instance->urlencode( "~ ~", strlen("~ ~") );
	LOG_STREAM_DEBUG << "~ ~ encode[" << encode_data << "]" << LOG_EOM;
	curl_free(  encode_data );	
	
#if 0
    int32_t req_1 = curmgr_instance->add_request( "qzone.wegame.qq.com", &url_response );
	LOG_STREAM_DEBUG << "req_1 = " << req_1 << LOG_EOM;
    int32_t req_2 = curmgr_instance->add_request( "qzone.wegame.qq.com", &url_response );
	LOG_STREAM_DEBUG << "req_2 = " << req_2 << LOG_EOM;
    int32_t req_3 = curmgr_instance->add_request( "qzone.wegame.qq.com", &url_response );
	LOG_STREAM_DEBUG << "req_3 = " << req_3 << LOG_EOM;
    int32_t req_4 = curmgr_instance->add_request( "qzone.wegame.qq.com", &url_response );
	LOG_STREAM_DEBUG << "req_4 = " << req_4 << LOG_EOM;
    if( req_4 < 0 )
    {
        fprintf( stderr, "curmgr is full! req_4(%d)\n", req_4 );
    }

    while( t-- > 0 )
    {
        curmgr_instance->run();
        if( 4 == t )
        {
            curmgr_instance->cancel_request( req_1 );
            curmgr_instance->cancel_request( req_1 );
        }
        else if( 3 == t )
            curmgr_instance->cancel_request( req_2 );
    }

    //curmgr_instance.cancel_request( client3 );

	t = T;
    while( t-- > 0 )
    {
        curmgr_instance->run();
        sleep( 1 );
    }
#endif 

	for( int32_t index = 0; index < 3; ++index )
	{
		int32_t req = curmgr_instance->add_request( "qzone.wegame.qq.com", &url_response );
		if( req < 0 )
		{
			LOG_STREAM_ERROR << "index[" << index << "] request failed!" << LOG_EOM;
		}
		else
		{
			LOG_STREAM_DEBUG << "index[" << index << "] req=[" << req << "] ok!" << LOG_EOM;			
		}
		curmgr_instance->run();
		usleep( 1000 );
	}
	
	t = T;
    while( t-- > 0 )
    {
        curmgr_instance->run();
        sleep( 1 );
    }	
	
	for( int32_t index = 0; index < 3; ++index )
	{
		int32_t req = curmgr_instance->add_request( "qzone.wegame.qq.com", &url_response );
		if( req < 0 )
		{
			LOG_STREAM_ERROR << "index[" << index << "] request failed!" << LOG_EOM;
		}
		else
		{
			LOG_STREAM_DEBUG << "index[" << index << "] req=[" << req << "] ok!" << LOG_EOM;			
		}
		curmgr_instance->run();
		usleep( 1000 );
	}
	
	t = T;
    while( t-- > 0 )
    {
        curmgr_instance->run();
        sleep( 1 );
    }		
	
	delete curmgr_instance;
	flush_log();
	fini_log();
    fprintf( stderr, "curl_test exit\n" );

    return 0;
}


